import ctypes
from ctypes import wintypes
import win32con, win32api, win32gui

# 低层鼠标钩子回调
HOOKPROC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, wintypes.WPARAM, wintypes.LPARAM)

class MSLLHOOKSTRUCT(ctypes.Structure):
    _fields_ = [("pt", wintypes.POINT),
                ("mouseData", wintypes.DWORD),
                ("flags", wintypes.DWORD),
                ("time", wintypes.DWORD),
                ("dwExtraInfo", ctypes.c_void_p)]

user32 = ctypes.windll.user32
hHookMouse = None

@HOOKPROC
def LowLevelMouseProc(nCode, wParam, lParam):
    if nCode == win32con.HC_ACTION:
        ms = ctypes.cast(lParam, ctypes.POINTER(MSLLHOOKSTRUCT)).contents
        if wParam == win32con.WM_LBUTTONDOWN:
            print('旁听：左键按下', ms.pt.x, ms.pt.y)
        # 必须 CallNextHookEx，否则系统卡死
    return user32.CallNextHookEx(hHookMouse, nCode, wParam, lParam)

# 安装钩子
hHookMouse = user32.SetWindowsHookExW(win32con.WH_MOUSE_LL, LowLevelMouseProc,
                                      win32api.GetModuleHandle(None), 0)

# 消息循环
msg = wintypes.MSG()
while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
    user32.TranslateMessage(ctypes.byref(msg))
    user32.DispatchMessageW(ctypes.byref(msg))

# 卸载
user32.UnhookWindowsHookEx(hHookMouse)